//
//  BIDPropertyWorkViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/10.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDPropertyWorkViewController.h"
#import "BIDPropertyWorkConditionDetailViewController.h"
#import "BIDPropertyWorkConditionCell.h"
#import "BIDPropertyWorkerCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

static NSString *propertyWorkerListURL = @"propertyAndroid/listStaff.action";

static NSString *propertyWorkConditionListURL = @"propertyAndroid/list.action";

@interface BIDPropertyWorkViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource>
{
    //物业员工列表
    NSArray *_propertyWorkerArr;
    //物业工作情况
    NSArray *_propertyWorkConditionArr;
    BOOL _bRegister;
}
@end

@implementation BIDPropertyWorkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"物业工作";
    // Do any additional setup after loading the view from its nib.
    [_collectionView registerClass:[BIDPropertyWorkConditionCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionView setBackgroundColor:RGBACOLOR(230.f, 230.f, 237.f, 1.f)];
    [self prepareLayout];
    //
    _tableView.tableFooterView = [[UIView alloc] init];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    //获取工作情况
    [self fetchPropertyWorkConditionInfo];
    //获取工作人员联系方式列表
    [self fetchPropertyWorkerInfo];
}

- (void)fetchPropertyWorkConditionInfo
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], propertyWorkConditionListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@&page=%d&rows=%d", [AppDelegate getUserId], 1, 5];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            _propertyWorkConditionArr = [[NSArray alloc] initWithArray:responseDic[@"rows"]];
            [_collectionView reloadData];
            [self prepareLayout];
        }
    }];
}

- (void)fetchPropertyWorkerInfo
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], propertyWorkerListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@", [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id obj, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            if([obj isKindOfClass:[NSArray class]])
            {
                NSArray *arr = (NSArray *)obj;
                _propertyWorkerArr = [[NSArray alloc] initWithArray:arr];
                [_tableView reloadData];
            }
        }
    }];
}

- (void)prepareLayout
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    CGFloat itemWidth = 0;
    CGFloat itemHeight = screenSize.height*37/80;
    if(_propertyWorkConditionArr.count > 1)
    {
        itemWidth = screenSize.width / 5 * 4;
    }
    else
    {
        itemWidth = screenSize.width;
    }
    [flowLayout setItemSize:CGSizeMake(itemWidth, CGRectGetHeight(_collectionView.frame))];
    [flowLayout setMinimumLineSpacing:20];
    [flowLayout setMinimumInteritemSpacing:20];
    [flowLayout setSectionInset:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
    [_collectionView setCollectionViewLayout:flowLayout];
}

- (void)callWithPhone:(NSString *)phone
{
    NSString *str = [[NSString alloc] initWithFormat:@"tel://%@", phone];
    NSURL *url = [NSURL URLWithString:str];
    UIWebView *telWebView = [[UIWebView alloc] init];
    [telWebView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:telWebView];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _propertyWorkConditionArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDPropertyWorkConditionCell *workCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = _propertyWorkConditionArr[row];
    NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"titleImg"]];
    [workCell.imgView setImageWithURL:[NSURL URLWithString:strFilePath] placeholderImage:[UIImage imageNamed:SLIDE_PLACEHOLDER]];
    //workCell.imgView.image = [UIImage imageNamed:dic[@"imgName"]];
    workCell.titleLabel.text = dic[@"title"];
    workCell.timeLabel.text = dic[@"createTime"];
    cell = workCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    NSUInteger row = indexPath.row;
    NSDictionary *dic = _propertyWorkConditionArr[row];
    BIDPropertyWorkConditionDetailViewController *vc = [[BIDPropertyWorkConditionDetailViewController alloc] initWithNibName:@"BIDPropertyWorkConditionDetailViewController" bundle:nil];
    vc.propertyWorkID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _propertyWorkerArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDPropertyWorkerCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDPropertyWorkerCell *workerCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _propertyWorkerArr[row];
    workerCell.nameLabel.text = dic[@"name"];
    workerCell.phoneLabel.text = dic[@"telephone"];
    cell = workerCell;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 30.f)];
    [headerView setBackgroundColor:RGBACOLOR(239.f, 239.f, 239.f, 1.f)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16.f, 4.5f, 150.f, 21.f)];
    [label setTextColor:RGBACOLOR(50, 54, 59, 1.f)];
    [label setText:@"物业员工"];
    [label setFont:[UIFont systemFontOfSize:14.f]];
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _propertyWorkerArr[indexPath.row];
    [self callWithPhone:dic[@"phone"]];
}

@end
